//
//  HGPageContenView.m
//  PageController
//
//  Created by yu on 2018/4/6.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "HGPageContenView.h"
@interface HGPageContenView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)NSArray *childVcs;
@property (nonatomic, weak)UIViewController *parentVc;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign)CGFloat startOffsetX;
@property (nonatomic, assign)BOOL isForbidDelegate;
@end

@implementation HGPageContenView
static NSString *contentCellID = @"ContentCellID";
- (instancetype)initWithFrame:(CGRect)frame childViewControllers:(NSArray *)childVcs andParentViewController:(UIViewController *)parentVc {
    if (self = [super initWithFrame:frame]) {
        _childVcs = childVcs;
        _parentVc = parentVc;
    
        [self setupUI];
    }
    
    return self;
}


#pragma mark -- 设置UI
- (void)setupUI {
    
    for (UIViewController *childVc in _childVcs) {
        [_parentVc addChildViewController:childVc];
    }
    
    [self addSubview:self.collectionView];
    
}


- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = self.bounds.size;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:contentCellID];
        
    }
    return _collectionView;
}




#pragma mark -- UICollectionView代理相关
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _childVcs.count;
}


- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:contentCellID forIndexPath:indexPath];
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIViewController *childVc = _childVcs[indexPath.row];
    childVc.view.frame = cell.contentView.bounds;
    [cell.contentView addSubview:childVc.view];
    return cell;
}




- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _isForbidDelegate = NO; //滑动时，允许调用代理
    _startOffsetX = scrollView.contentOffset.x;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (_isForbidDelegate) {
        return;
    }
    NSLog(@"代理");
    CGFloat progress = 0;
    NSInteger sourceIndex = 0;
    NSInteger targetIndex = 0;
    
    //1.判断左滑还是右滑
    CGFloat currentOffsetX = scrollView.contentOffset.x;
    CGFloat scrollViewW = scrollView.frame.size.width;
    
    if (currentOffsetX > _startOffsetX) {//左滑
        //1.计算progress
        progress = (currentOffsetX / scrollViewW) - floor(currentOffsetX / scrollViewW);
        
        //2.计算sourceIndex
        sourceIndex = (NSInteger)(currentOffsetX / scrollViewW);
        
        //3.计算targetIndex
        targetIndex = sourceIndex + 1;
        if (targetIndex >= self.childVcs.count) {
            targetIndex = self.childVcs.count - 1;
        }
        
        if (currentOffsetX - _startOffsetX == scrollViewW) {
            progress = 1;
            targetIndex = sourceIndex;
        }
    }else {//右滑
        
        //1.计算progress
        progress = 1 - ((currentOffsetX / scrollViewW) - floor(currentOffsetX / scrollViewW));
        
        //2.计算targetIndex
        targetIndex = (NSInteger)(currentOffsetX / scrollViewW);
        
        //计算sourceIndex
        sourceIndex = targetIndex + 1;
        if (sourceIndex >= self.childVcs.count) {
            sourceIndex = self.childVcs.count - 1;
        }
        
    }
    NSLog(@"p: %lf, s: %ld, t: %ld", progress, sourceIndex, targetIndex);
    if ([self.delegate respondsToSelector:@selector(pageContenViewScrollProgress:sourceIndex:targetIndex:)]) {
        [self.delegate pageContenViewScrollProgress:progress sourceIndex:sourceIndex targetIndex:targetIndex];
    }
}


#pragma mark -- 滚动
- (void)pageContentViewScrollToCurrentIndex:(NSInteger)index {
    _isForbidDelegate = YES; //点击事件，禁止调用代理
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.size.width * index, 0)];
}


@end
